package leetcode.每日一题;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author :songyaozhang
 * @date :Created 2021/7/14 16:25
 * @description :
 */
public class 质数乘积 {

    @Test
    public void Test() {
        Solution solution = new Solution();
        System.out.println(solution.primeProduct(500));
    }

    class Solution {
        List<Integer> list = new ArrayList<>();
        public String primeProduct(int n) {
            divide(n);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                builder.append(list.get(i));
                if (i != list.size() - 1) {
                    builder.append("*");
                }
            }
            return builder.toString();
        }

        private void divide(int n) {
            for (int i = 2; i <= n / i; i++) {
                if (n % i == 0) {
                    int cnt = 0;
                    while (n % i == 0) {
                        cnt++;
                        n /= i;
                    }
                    while (cnt-- > 0) list.add(i);
                }
            }
            if (n > 1) list.add(n);
        }
    }
}
